package com.murfrosoft.airhockey.game;

import java.util.ArrayList;
import java.util.List;

import com.murfrosoft.airhockey.entities.CompPaddle;
import com.murfrosoft.airhockey.entities.CompPaddleLevel1;
import com.murfrosoft.airhockey.entities.CompPaddleLevel2;
import com.murfrosoft.airhockey.entities.CompPaddleLevel3;
import com.murfrosoft.airhockey.entities.Puck;
import com.murfrosoft.airhockey.entities.UserPaddle;
import com.murfrosoft.airhockey.services.CollisionDetectorService;
import com.murfrosoft.airhockey.services.CollisionDetectorServiceImpl;
import com.murfrosost.airhockey.R.drawable;
import com.murfrosost.airhockey.activities.MainActivity;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.Paint.Align;
import android.os.Build;
import android.os.Handler;
import android.view.WindowManager;




public class AirHockeyGame2 extends SingleTouchGameEngine{
	/* Declare screen size variables ??? (can we auto-detect this?) */
	int HEIGHT = 200;
	int WIDTH = 300;
	
	Bitmap gameBoard;
	
	int score1 = 0;
	int score2 = 0;
	
	CollisionDetectorService service;
	
	Puck puck;
	CompPaddle compPaddle;
	UserPaddle userPaddle;
	
	Handler handler;
	Context context;
	
	final int MAX_SCORE = 5;
	GameState gameState;
	int killCounter = 0;
	final int KILL_TIME = 400;
	
	Paint flashPaint = new Paint();
	
	public AirHockeyGame2(Context context,int width,int height) {
		super(context);
		flashPaint.setColor(Color.BLUE);
		flashPaint.setTextAlign(Align.CENTER);
		flashPaint.setAntiAlias(true);
		flashPaint.setTextSize(40f);
		handler = new Handler();
		this.context=context;
		gameState = GameState.PLAY;
		

		WIDTH=width;
		HEIGHT=height;
		
		service=new CollisionDetectorServiceImpl();
		

		BitmapFactory.Options options= new BitmapFactory.Options();
		options.inPreferredConfig=Bitmap.Config.ARGB_8888;

		
		Bitmap bitmap=BitmapFactory.decodeResource(getResources(), drawable.rink,options);
		gameBoard = Bitmap.createScaledBitmap(bitmap, WIDTH, HEIGHT, false);
		
		
		
		
		puck = new Puck(context);
		puck.setRadius((int)WIDTH/15);
		puck.setFriction(.99f);
		puck.setPositionX(WIDTH/2);
		puck.setPositionY(HEIGHT/2);
		puck.setVelocityY(0);
		puck.setVelocityX(0);
		bitmap = BitmapFactory.decodeResource(getResources(),drawable.puck,options);
		Bitmap puckBitmap = Bitmap.createScaledBitmap(bitmap, (int)puck.getRadius()*2, (int)puck.getRadius()*2, false);
		puck.setBitmap(puckBitmap);
		
		
		switch(GameStats.getLevel()){
		case 1:compPaddle=new CompPaddleLevel1();
		break;
		case 2:compPaddle=new CompPaddleLevel2();
		break;
		case 3:compPaddle=new CompPaddleLevel3();
		break;
		case 4:compPaddle=new CompPaddle();
		break;
		case 5:compPaddle=new CompPaddleLevel1();
		break;
		
		}
		compPaddle.setRadius(WIDTH/10);
		compPaddle.setPositionX(200);
		compPaddle.setPositionY(100);
		compPaddle.setVelocityX(2);
		compPaddle.setVelocityY(5);
		bitmap=BitmapFactory.decodeResource(getResources(),drawable.paddle_b, options);
		Bitmap paddleBlue= Bitmap.createScaledBitmap(bitmap, (int)compPaddle.getRadius()*2, (int)compPaddle.getRadius()*2, false);
		compPaddle.setBitmap(paddleBlue);
		
		userPaddle = new UserPaddle();
		userPaddle.setRadius(WIDTH/10);
		userPaddle.setOldPositionX(100);
		userPaddle.setOldPositionY(600);
		userPaddle.setPositionX(100);
		userPaddle.setOldPositionY(600);
		userPaddle.setVelocityX(0);
		userPaddle.setVelocityY(0);
		
	}



	@Override
	public void updatePhysics() {
		/* UPDATE PUCK POSITION */
		int s;
		
        compPaddle.move(WIDTH, HEIGHT, puck.getPositionX(), puck.getPositionY());
        userPaddle.move(WIDTH, HEIGHT, touch.x, touch.y);
    	puck.move(WIDTH,HEIGHT);
	

		puck.do_collision3(compPaddle);
		puck.do_collision3(userPaddle);		// trying some new collision math.
		
		s = puck.check_score(WIDTH, HEIGHT);
		if( s == -1 ){
			score2 += 1;
			puck.reset(WIDTH, HEIGHT, 2);
		}
		else if( s == 1 ){
			score1 += 1;
			puck.reset(WIDTH, HEIGHT, 1);
		}
		if(score1>=MAX_SCORE){
			gameState=GameState.LOSE;
		}
		if(score2>=MAX_SCORE){
			gameState=GameState.WIN;
		}
		
		if(gameState==GameState.LOSE||gameState==GameState.WIN){
			killCounter++;
		}
		
		
		
		
		if(killCounter>=KILL_TIME&&gameState==GameState.LOSE){
			GameStats.loseMan();
			if(GameStats.getLives()<1){
				GameStats.reset();
				//goto end screen
			}
			returnToSplashScreen();
			
		}
		if(killCounter>=KILL_TIME&&gameState==GameState.WIN){
			GameStats.advanceLevel();
			if(GameStats.getLevel()>10){
				//goto win screen
			}
			returnToSplashScreen();
			
		}
		
		/* CHECK FOR COLLISIONS WITH OUR PADDLE */
        //service.checkForCollision(puck, userPaddle);
		
		/* Check for collision with computer paddle */

		//service.checkForCollision(puck, compPaddle);

		
	}
	

	@Override
	public void draw(Canvas canvas) {
    
		/* NOTES:  James, I don't know where to place globals, or new classes, 
		 *   		in this code.  I'm doing a practice run using non-objects in the
		 *          draw method.  This is messy and inefficient, but should be playable
		 *          as a mock-up.  I'll need your help to do this properly!
		 */
		
		/* Draw Gameboard */
		Paint paint = new Paint();
		canvas.drawBitmap(gameBoard,0,0,paint);
		
		// Add center line
		Paint line_color = new Paint();
		line_color.setAntiAlias(true);
		
		line_color.setColor(Color.rgb(0,180,0)); // green
		line_color.setAlpha(100);
		canvas.drawCircle(WIDTH/2, HEIGHT/2, WIDTH/4, line_color);
		canvas.drawRect(0, HEIGHT/2+4, WIDTH,  HEIGHT/2-4, line_color);
		// computer goal area
		line_color.setColor(Color.rgb(0,0,180)); 
		line_color.setAlpha(100);
		canvas.drawCircle(WIDTH/2, 0, WIDTH/4, line_color);
		// player goal area
		line_color.setColor(Color.rgb(180,0,0)); 
		line_color.setAlpha(100);
		canvas.drawCircle(WIDTH/2, HEIGHT, WIDTH/4, line_color);
			
	    //canvas.drawColor(Color.rgb(200,200,200));

		paint.setColor(Color.BLACK);
		
		/* DEBUG TEXT 
		paint.setTextSize(20);
		canvas.drawText("X:" + touch.x +" Y:" + touch.y, 20, 20, paint);
		canvas.drawText("Xv:" + userPaddle.getVelocityX() + " Yv:" + userPaddle.getVelocityY(), 20,40, paint);
		canvas.drawText(puck.getVelocityX() + "  " + puck.getVelocityY(), 20, 60, paint);
		*/
		
		/* SCORE TEXT */
		paint.setAntiAlias(true);
		paint.setTextSize(50);
		paint.setColor(Color.BLUE);
		canvas.drawText("" + score1, 0.10f*WIDTH, 0.4f*HEIGHT+50, paint);
		paint.setColor(Color.RED);
		canvas.drawText("" + score2, 0.10f*WIDTH, 0.6f*HEIGHT, paint);
	
		/* DRAW PUCK & PADDLES */
		puck.draw(canvas);
		compPaddle.draw(canvas);
		userPaddle.draw(canvas);

		
		if(gameState==GameState.WIN){
		    if(flashPaint.getColor()==Color.BLUE){
            	flashPaint.setColor(Color.BLACK);
            }else{
            	flashPaint.setColor(Color.BLUE);
            }
			canvas.drawText("You Win!",WIDTH/2,HEIGHT/2,flashPaint);
		}
		else if(gameState==GameState.LOSE){
            if(flashPaint.getColor()==Color.BLUE){
            	flashPaint.setColor(Color.BLACK);
            }else{
            	flashPaint.setColor(Color.BLUE);
            }
			canvas.drawText("You Lose!",WIDTH/2,HEIGHT/2,flashPaint);
		
		}
		
	
		
	}
	
	
	public void returnToSplashScreen(){
		handler.post(new Runnable(){
			@Override
			public void run(){
		    Activity a = (Activity)context;
		    a.finish();
			}
		});
	}

}
